package com.dq.context.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

import static com.xiaoleilu.hutool.collection.CollUtil.newArrayList;

/**
 * @创建人 gewenzhen
 * @创建时间 2018/12/21
 * @描述  SwaggerConf  配置拦截路径
 */
@Configuration
@EnableSwagger2
public class SwaggerConf {
    @Bean
    public Docket opBoss() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("opboss")// 定义组
                .select() // 选择那些路径和 api 会生成 document
                .apis(RequestHandlerSelectors.basePackage("com.dq.controller.op.opboss")) // 拦截的包路径
                .paths(PathSelectors.any())
                .build() // 创建
                .globalOperationParameters(setHeaderToken())
                .apiInfo(apiInfo()) // 配置说明
                .securitySchemes(security());
    }

    @Bean
    public Docket opMerchant() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("opmerchant")// 定义组
                .select() // 选择那些路径和 api 会生成 document
                .apis(RequestHandlerSelectors.basePackage("com.dq.controller.op.opmerchant")) // 拦截的包路径
                .paths(PathSelectors.any())
                .build() // 创建
                .globalOperationParameters(setHeaderToken())
                .apiInfo(apiInfo()) // 配置说明
                .securitySchemes(security());
    }

    @Bean
    public Docket annex() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("annex")// 定义组
                .select() // 选择那些路径和 api 会生成 document
                .apis(RequestHandlerSelectors.basePackage("com.dq.controller")) // 拦截的包路径
                .paths(PathSelectors.any())
                .build() // 创建
                .globalOperationParameters(setHeaderToken())
                .apiInfo(apiInfo())// 配置说明
                .securitySchemes(security());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()//
                .title("东启")// 标题
                .description("基础模块api")// 描述
                .termsOfServiceUrl("http://www.xiaoniulianmeng.com")//
                .version("1.0")// 版本
                .build();
    }

    private List<Parameter> setHeaderToken() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        tokenPar.name("X-Dqmall-Token").description("token").modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(tokenPar.build());
        return pars;
    }
    private List<ApiKey> security() {
        return newArrayList(
                new ApiKey("X-Dqmall-Token", "X-Dqmall-Token", "header")
        );
    }

}
